package com.ruoyi.web.controller.market.product;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.db.domain.ProductProfit;
import com.ruoyi.db.model.excle.ProductProfitAddupExcel;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.product.service.IProductProfitService;
import com.ruoyi.product.service.ISceneInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.ProductProfitAddup;
import com.ruoyi.product.service.IProductProfitAddupService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.ProductProfitAddupWapper;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * profitAddupController
 * 
 * @author ruoyi
 * @date 2022-08-31
 */
@Controller
@RequestMapping("/product/profitAddup")
public class ProductProfitAddupController extends BaseController
{
    private String prefix = "product/profitAddup";

    @Autowired
    private IProductProfitAddupService productProfitAddupService;
    @Autowired
    private IProductProfitService productProfitService;
    @Autowired
    private IChannelInfoService channelInfoService;

    @Autowired
    private ISceneInfoService sceneInfoService;

    @RequiresPermissions("product:profitAddup:view")
    @GetMapping("/{id}")
    public String profitAddup(@PathVariable("id") String id,
                              @RequestParam(value = "chCode", required = false) String chCode,
                              ModelMap mmap)
    {

        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        List<Map<String, String>> channelInfoList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
        List<Map<String, Object>> sceneInfoList = sceneInfoService.listMap();

        if (StrUtil.isNotEmpty(chCode)){
            channelInfoList.forEach(map -> {
                String value = map.get("value");
                if (StrUtil.equals(value, chCode)){
                    map.put("checked", "1");
                }else{
                    map.put("checked", "0");
                }
            });
        }

        mmap.put("channelInfoList", channelInfoList);
        mmap.put("sceneInfoList", sceneInfoList);

        mmap.put("profitId", id);
        mmap.put("chCode", chCode);
        return prefix + "/profitAddup";
    }

    /**
     * 查询profitAddup列表
     */
    @RequiresPermissions("product:profitAddup:view")
    @PostMapping("/list/{profitId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("profitId") String profitId,
                              @RequestParam(value = "chCode", required = false) String chCode,
                              ProductProfitAddup productProfitAddup)
    {

        ProductProfit productProfit = productProfitService.selectProductProfitById(profitId);
        productProfitAddup.setDateInfo(productProfit.getDateInfo());
        productProfitAddup.setProductId(productProfit.getProductId());
        if (StrUtil.isEmpty(productProfitAddup.getChannelCode()) && StrUtil.isNotEmpty(chCode)){
            productProfitAddup.setChannelCode(chCode);
        }

        startPage();
        List<Map<String, Object>> mapList = productProfitAddupService.selectProductProfitAddupListMap(productProfitAddup);
        mapList = new ProductProfitAddupWapper(mapList).wrap();
        return getDataTable(mapList);
    }


    @RequiresPermissions("product:profitAddup:total")
    @GetMapping("/total/{id}")
    public String profitAddupTotal(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("profitId", id);
        return prefix + "/profitAddupTotal";
    }

    /**
     * 查询profitAddup列表
     * 统计列表
     */
    @RequiresPermissions("product:profitAddup:view")
    @PostMapping("/totalList/{profitId}")
    @ResponseBody
    public TableDataInfo totalList(@PathVariable("profitId") String profitId)
    {
        List<Map<String, Object>> mapList = productProfitAddupService.selectProductProfitAddupTotalListMap(profitId);
        return getDataTable(mapList);
    }



    /**
     * 导出profitAddup列表
     */
    @Log(title = "profitAddup", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{profitId}")
    @ResponseBody
    public AjaxResult export(@PathVariable("profitId") String profitId)
    {

        ProductProfitAddup productProfitAddup = new ProductProfitAddup();

        ProductProfit productProfit = productProfitService.selectProductProfitById(profitId);
        productProfitAddup.setDateInfo(productProfit.getDateInfo());
        productProfitAddup.setProductId(productProfit.getProductId());

        List<ProductProfitAddupExcel> list = productProfitAddupService.selectProductProfitAddupList(productProfitAddup);
        ExcelUtil<ProductProfitAddupExcel> util = new ExcelUtil<ProductProfitAddupExcel>(ProductProfitAddupExcel.class);
        return util.exportExcel(list, "产品uv数据");
    }

    /**
     * 新增profitAddup
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存profitAddup
     */
    @RequiresPermissions("product:profitAddup:add")
    @Log(title = "profitAddup", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductProfitAddup productProfitAddup)
    {
        return toAjax(productProfitAddupService.insertProductProfitAddup(productProfitAddup));
    }

    /**
     * 修改profitAddup
     */
    @RequiresPermissions("product:profitAddup:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ProductProfitAddup productProfitAddup = productProfitAddupService.selectProductProfitAddupById(id);
        mmap.put("productProfitAddup", productProfitAddup);
        return prefix + "/edit";
    }

    /**
     * 修改保存profitAddup
     */
    @RequiresPermissions("product:profitAddup:edit")
    @Log(title = "profitAddup", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductProfitAddup productProfitAddup)
    {
        return toAjax(productProfitAddupService.updateProductProfitAddup(productProfitAddup));
    }

    /**
     * 删除profitAddup
     */
    @RequiresPermissions("product:profitAddup:remove")
    @Log(title = "profitAddup", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productProfitAddupService.deleteProductProfitAddupByIds(ids));
    }
}
